package com.itc.dispatch.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itc.common.core.domain.R;
import com.itc.common.core.utils.OkHttpUtil;
import com.itc.dispatch.domain.FlightOperationMonitoring;
import com.itc.dispatch.domain.FlightRecordLimit;
import com.itc.dispatch.domain.dto.LimitRecordFieldDto;
import com.itc.dispatch.service.FlightOperationMonitoringService;
import com.itc.dispatch.service.FlightRecordLimitService;
import com.itc.dispatch.mapper.FlightRecordLimitMapper;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 91516
* @description 针对表【flight_record_limit(航班运行监控限制录入信息表)】的数据库操作Service实现
* @createDate 2024-08-21 14:34:42
*/
@Service
public class FlightRecordLimitServiceImpl extends ServiceImpl<FlightRecordLimitMapper, FlightRecordLimit>
    implements FlightRecordLimitService{

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private FlightOperationMonitoringService flightOperationMonitoringService;

    @Override
    public R getList(FlightRecordLimit dto){
        //api获取限制录入数据
//        Object resultObj = OkHttpUtil.sendOkHttpClient("http://172.23.80.2:8080/alg/ptd/getParams"
//                , "GET",null,null);
//        if(resultObj == null){
//            return R.fail("获取数据失败");
//        }
//        JSONObject dataJson = JSONObject.parseObject(resultObj.toString());
//        if(!"200".equals(dataJson.getString("code"))){
//            return R.fail(dataJson.getString("msg"));
//        }
        try{
//            List<FlightRecordLimit> limitList = objectMapper.readValue(dataJson.getString("data")
//                    ,new TypeReference<List<FlightRecordLimit>>(){});
//
//            this.baseMapper.delete(
//                    new LambdaQueryWrapper<FlightRecordLimit>()
//                    .notIn(FlightRecordLimit::getRecordTime,limitList.stream().map(FlightRecordLimit::getRecordTime).collect(Collectors.toList()))
//            );
            List<FlightRecordLimit> dataList = this.baseMapper.selectList(null);

            return R.ok(dataList);
        }catch (Exception e){
            e.printStackTrace();
            return R.fail("数据解析异常");
        }
    }

    @Override
    public R saveData(FlightRecordLimit dto) {
        Gson gson = new Gson();
        dto.setDataJson(gson.toJson(dto.getDataJsonFormat()));

        //查询符合条件的航班ID信息
        QueryWrapper<FlightOperationMonitoring> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("atd_d");
        if(StringUtils.equals("1",dto.getDataJsonType())){
            for (LimitRecordFieldDto limitRecordFieldDto : dto.getDataJsonFormat()) {
                formatQueryParam(queryWrapper,limitRecordFieldDto,1);
            }
        }else{
            queryWrapper.and(warry -> {
                for (LimitRecordFieldDto limitRecordFieldDto : dto.getDataJsonFormat()) {
                    formatQueryParam(warry,limitRecordFieldDto,2);
                    warry.or();
                }
            });

        }
        List<FlightOperationMonitoring> flightList = flightOperationMonitoringService.getBaseMapper().selectList(queryWrapper);
        dto.setControlFlightList(flightList.stream().map(FlightOperationMonitoring::getFlightIdD).collect(Collectors.joining(",")));
        this.baseMapper.insert(dto);

        List<FlightRecordLimit> dataList = this.baseMapper.selectList(null);
        if(dataList.size() == 0){
            return R.ok();
        }
        JSONArray bigData = new JSONArray();
        for (FlightRecordLimit flightRecordLimit : dataList) {
            String jsonString = gson.toJson(flightRecordLimit);
            // 将JSON字符串转换为JSONObject
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            bigData.add(jsonObject);
        }

        //同步至大数据平台
//        RequestBody body = RequestBody.create(bigData.toJSONString(), MediaType.parse("application/json"));
//        Object ob = OkHttpUtil.sendOkHttpClient("http://172.23.80.2:8080/alg/ptd/updateParams","POST",body,null);
//        System.out.println(ob);
        return R.ok();
    }

    /**
     * 组装查询参数
     * @param queryWrapper
     * @param limitRecordFieldDto
     */
    public void formatQueryParam(QueryWrapper<FlightOperationMonitoring> queryWrapper,LimitRecordFieldDto limitRecordFieldDto,int type){
        int compareType = limitRecordFieldDto.getCompareType();
        if(compareType == 1){
            queryWrapper.eq(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }else if(compareType == 2){
            queryWrapper.ne(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }else if(compareType == 3){
            queryWrapper.like(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }else if(compareType == 4){
            queryWrapper.notLike(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }else if(compareType == 5){
            queryWrapper.like(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }else if(compareType == 6){
            queryWrapper.notLike(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }else if(compareType == 7){
            queryWrapper.isNull(limitRecordFieldDto.getFieldUnderName());
        }else if(compareType == 8){
            queryWrapper.isNotNull(limitRecordFieldDto.getFieldUnderName());
        }else if(compareType == 10){
            queryWrapper.gt(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }else if(compareType == 11){
            queryWrapper.ge(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }else if(compareType == 12){
            queryWrapper.gt(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }else if(compareType == 13){
            queryWrapper.lt(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }else if(compareType == 14){
            queryWrapper.le(limitRecordFieldDto.getFieldUnderName(),limitRecordFieldDto.getCompareValue());
        }
    }
}

